/**
 * Copyright &copy; 2018-2018 <a href="#">聚成科技</a> All rights reserved.
 */
package com.jckj.core.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jckj.core.annotation.CurrentDataSource;
import com.jckj.core.dao.FarmParkMapper;
import com.jckj.core.dao.FarmParkPersonMapper;
import com.jckj.core.dao.SysUserMapper;
import com.jckj.core.db.DataSourceEnum;
import com.jckj.core.entity.*;
import com.jckj.core.enums.FarmParkStatus;
import com.jckj.core.exception.ServiceException;
import com.jckj.core.service.ParkService;
import com.jckj.core.util.StringUtils;
import com.jckj.core.vos.FarmParkQueryVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *     园区管理服务类
 * </p>
 *
 * @author 刘志远
 * @date 18-1-24
 */
@Service
@Transactional(readOnly = true, rollbackFor = ServiceException.class)
public class ParkServiceImpl implements ParkService {

    private static Logger LOGGER = LoggerFactory.getLogger(ParkServiceImpl.class);

    @Autowired
    FarmParkMapper farmParkMapper;

    @Autowired
    SysUserMapper sysUserMapper;

    @Autowired
    FarmParkPersonMapper farmParkPersonMapper;

    @Override
    @Transactional(readOnly = false, rollbackFor = ServiceException.class)
    public int add(FarmPark farmPark) throws ServiceException {
        int effectCount = 0;
        try{
            effectCount = farmParkMapper.insert(farmPark);
        }catch(Exception ex){
            LOGGER.error("新增园区:", ex.fillInStackTrace());
            throw new ServiceException(ex);
        }
        return effectCount;
    }

    @Override
    @Transactional(readOnly = false, rollbackFor = ServiceException.class)
    public void update(FarmPark farmPark) {
        try{
            farmParkMapper.updateByPrimaryKeySelective(farmPark);
        }catch(Exception ex) {
            LOGGER.error("ParkServiceImpl-update", ex.fillInStackTrace());
            throw new ServiceException(ex);
        }
    }

    @Override
    @Transactional(readOnly = false, rollbackFor = ServiceException.class)
    public void delete(FarmPark farmPark) throws ServiceException {
        FarmPark origin = farmParkMapper.selectByPrimaryKey(farmPark.getId());
        if(origin==null){
            throw new ServiceException("要删除的园区不存在");
        }
        if(FarmParkStatus.DELETED.getCode().equals(farmPark.getStatus())){
            throw new ServiceException("要删除的园区已是删除状态不可重复操作");
        }
        farmParkMapper.updateByPrimaryKeySelective(farmPark);

    }

    @Override
    @Transactional(readOnly = false, rollbackFor = ServiceException.class)
    public int addAdmin(SysUser sysUser, FarmPark farmPark) throws ServiceException {
        int effectCount = 0;
        try{
            sysUserMapper.insert(sysUser);
            FarmParkPersonKey farmParkPersonKey = new FarmParkPersonKey();
            farmParkPersonKey.setParkId(farmPark.getId());
            farmParkPersonKey.setUserId(sysUser.getId());
            farmParkPersonMapper.insert(farmParkPersonKey);
            effectCount = farmParkMapper.updateByPrimaryKeySelective(farmPark);
        }catch(Exception ex){
            LOGGER.error("增加园区管理员:", ex.fillInStackTrace());
            throw new ServiceException(ex);
        }
        return effectCount;
    }

    @Override
    @Transactional(readOnly = false, rollbackFor = ServiceException.class)
    public int lock(FarmPark farmPark) throws ServiceException {
        int effectCount = 0;
        try{
            effectCount = farmParkMapper.updateByPrimaryKey(farmPark);
        }catch(Exception ex){
            LOGGER.error("增加园区管理员:", ex.fillInStackTrace());
            throw new ServiceException(ex);
        }
        return effectCount;
    }

    @Override
    public List<FarmPark> findAll() {
        FarmParkExample farmParkExample = new FarmParkExample();
        FarmParkExample.Criteria criteria = farmParkExample.createCriteria();
        criteria.andStatusEqualTo(FarmParkStatus.NORMAL.getCode());
        return farmParkMapper.selectByExample(farmParkExample);
    }

    @Override
    @CurrentDataSource(DataSourceEnum.SLAVE)
    public long checkParkNo(FarmParkQueryVo farmParkQueryVo) throws ServiceException {
        FarmParkExample example = new FarmParkExample();
        FarmParkExample.Criteria  criteria = example.createCriteria();
        criteria.andParkNoEqualTo(farmParkQueryVo.getParkNo());
        return farmParkMapper.countByExample(example);
    }

    @Override
    @CurrentDataSource(DataSourceEnum.SLAVE)
    public PageInfo<FarmPark> findByPage(FarmParkQueryVo farmParkQueryVo) throws ServiceException {
        Page<FarmPark> page =PageHelper.startPage(farmParkQueryVo.getPage(), farmParkQueryVo.getLimit());
        FarmParkExample farmParkExample = new FarmParkExample();
        FarmParkExample.Criteria criteria = farmParkExample.createCriteria();
        if(StringUtils.isNotBlank(farmParkQueryVo.getParkNo())){
            criteria.andParkNoLike(farmParkQueryVo.getParkNo());
        }
        if(StringUtils.isNotBlank(farmParkQueryVo.getParkName())){
            criteria.andParkNameLike(farmParkQueryVo.getParkName() + "%");
        }
        if(StringUtils.isNotEmpty(farmParkQueryVo.getStatus())){
            criteria.andStatusEqualTo(farmParkQueryVo.getStatus());
        }
        List<FarmPark> parks = farmParkMapper.selectByExample(farmParkExample);
        PageInfo<FarmPark> pageInfo = new PageInfo<>(page);
        pageInfo.setList(parks);
        return pageInfo;
    }

    @Override
    @CurrentDataSource(DataSourceEnum.SLAVE)
    public FarmPark querySingleFramParkById(String parkId) {
        return farmParkMapper.selectByPrimaryKey(parkId);
    }

    @Override
    @Transactional(readOnly = false, rollbackFor = ServiceException.class)
    public void disabledFarmPark(FarmPark farmPark) {
        FarmPark origin = farmParkMapper.selectByPrimaryKey(farmPark.getId());
        if(origin==null){
            throw new ServiceException("要禁用的园区不存在");
        }
        if(!FarmParkStatus.NORMAL.getCode().equals(origin.getStatus())){
            throw new ServiceException("要操作的园区状态不正确");
        }
        farmParkMapper.updateByPrimaryKeySelective(farmPark);
    }

    @Override
    @Transactional(readOnly = false, rollbackFor = ServiceException.class)
    public void enabledFarmPark(FarmPark farmPark) {
        FarmPark origin = farmParkMapper.selectByPrimaryKey(farmPark.getId());
        if(origin==null){
            throw new ServiceException("要禁用的园区不存在");
        }
        if(!FarmParkStatus.LOCKED.getCode().equals(origin.getStatus())){
            throw new ServiceException("要操作的园区状态不正确");
        }
        farmParkMapper.updateByPrimaryKeySelective(farmPark);
    }

}
